package com.medic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.medic.entity.Admin;
import com.medic.mapper.interf.AdminMapper;
import com.medic.service.interf.AdminServiceInterface;

@Service("adminService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) // 注明需要回滚
public class AdminServiceImpl implements AdminServiceInterface {

	@Autowired
	private AdminMapper adminMapper;

	@Override
	public boolean save(Admin object) throws Exception {
		boolean result;
		if (adminMapper.save(object) > 0) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public boolean delete(Admin object) throws Exception {
		boolean result;
		if (adminMapper.delete(object) > 0) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public boolean update(Admin object) throws Exception {
		boolean result;
		if (adminMapper.update(object) > 0) {
			result = true;
		} else {
			result = false;
		}
		return result;
	}

	@Override
	public List<Admin> getByPage(int current, int number, Admin admin) throws Exception {
		current = (current - 1) * number;
		return adminMapper.selectByPage(current, number, admin);
	}

	@Override
	public Admin login(String adminCode, String adminPass) throws Exception {
		Admin admin = adminMapper.selectByCode(adminCode);
		if (admin == null || !admin.getAdminPass().equals(adminPass)) {
			admin = null;
		} else {
			//将密码设为""保证安全
			admin.setAdminPass("");
		}
		return admin;
	}

	@Override
	public boolean check(String adminCode) throws Exception {
		boolean result = false;
		if (adminMapper.selectByCode(adminCode) != null) {
			result = true;
		}
		return result;
	}
}
